package com.smart.upms.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.utils.CopyUtils;
import com.smart.core.starter.utils.TreeUtil;
import com.smart.upms.sys.entity.SysDept;
import com.smart.upms.sys.mapper.SysDeptMapper;
import com.smart.upms.sys.param.SysDeptParam;
import com.smart.upms.sys.param.SysDeptQueryParam;
import com.smart.upms.sys.result.DeptTreeResult;
import com.smart.upms.sys.result.SysDeptResult;
import com.smart.upms.sys.service.ISysDeptService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门 服务实现类
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Service
@AllArgsConstructor
public class SysDeptServiceImpl implements ISysDeptService {

    private final SysDeptMapper mapper;


    @Override
    public Boolean save(SysDeptParam param) {
        SysDept entity = CopyUtils.copyObject(param, SysDept.class);
        int insert = mapper.insert(entity);
        return insert > 0;
    }

    @Override
    public Boolean update(SysDeptParam param) {
        SysDept entity = CopyUtils.copyObject(param, SysDept.class);
        int update = mapper.updateById(entity);
        return update > 0;

    }

    @Override
    public Boolean delete(Long id) {
        int delete = mapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public SysDeptResult get(Long id) {
        SysDept entity = mapper.selectById(id);
        return CopyUtils.copyObject(entity, SysDeptResult.class);
    }

    @Override
    public List<SysDeptResult> list(SysDeptQueryParam param) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        List<SysDept> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysDeptResult.class);
    }

    @Override
    public Page<SysDeptResult> listPage(Page<SysDeptResult> page, SysDeptQueryParam param) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        if (param.getParentId() != null) {
            queryWrapper.eq(SysDept.PARENT_ID, param.getParentId());
        }
        if (StringUtils.isNotBlank(param.getName())) {
            queryWrapper.likeRight(SysDept.NAME, param.getName());
        }
        Page<SysDept> entityPage = new Page<>();
        entityPage.setSize(page.getSize());
        entityPage.setCurrent(page.getCurrent());
        entityPage.setAsc(page.ascs());
        entityPage.setDesc(page.descs());
        mapper.selectPage(entityPage, queryWrapper);
        Page<SysDeptResult> resultPage = CopyUtils.copyPage(entityPage, SysDeptResult.class);
        return resultPage;
    }

    @Override
    public List<DeptTreeResult> selectListTree() {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(SysDept.ORDER_NUM);
        List<SysDept> list = mapper.selectList(queryWrapper);
        return getDeptTree(list, 0L);
    }


    /**
     * 构建部门树
     *
     * @param depts 部门
     * @return
     */
    private List<DeptTreeResult> getDeptTree(List<SysDept> depts, long parentId) {

        List<DeptTreeResult> treeList = depts.stream()
                .filter(dept -> !dept.getId().equals(dept.getParentId()))
                .map(dept -> {
                    DeptTreeResult node = new DeptTreeResult();
                    node.setId(dept.getId());
                    node.setParentId(dept.getParentId());
                    node.setName(dept.getName());
                    return node;
                }).collect(Collectors.toList());
        return TreeUtil.bulid(treeList, parentId);
    }

}
